package com.ifengchao.service.impl;

import com.ifengchao.dao.iface.DealReptileDao;
import com.ifengchao.pojo.HConfig;
import com.ifengchao.pojo.HCrawlLog;
import com.ifengchao.pojo.HMessagePlat;
import com.ifengchao.service.DealWeChatService;
import com.ifengchao.util.ReptileUtil;
import com.ifengchao.util.WechatUtil;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.ifengchao.util.DateUtils.getDayBegin;


/**
 * @Author YiMing
 * @Description:
 * @Date: Created in 上午10:53 2017/9/15
 * @Modified By
 */
@Service
public class DealWeChatServiceImpl implements DealWeChatService {

	private static final Logger logger = Logger.getLogger(DealWeChatServiceImpl.class);

	@Autowired(required = true)
	private DealReptileDao dealReptileDao;

	private static String hyzUrl = null; //慧驿站url

	@Override
	public void getWeChatData() {

		//查询爬虫配置开关是否开启
		Map<String, String> map = dealReptileDao.querySetting();

		String flag = map.get("reptile_thread_flag");

		hyzUrl = map.get("hyzUrl");

		logger.info("开关状态是:" + flag);

		//用于存储爬虫运行时间list
		ArrayList<HCrawlLog> hCrawlLogs = new ArrayList<>();

		if ("Y".equals(flag)) {
			List<HConfig> hConfigList = dealReptileDao.queryConfig();
			for (HConfig hConfig : hConfigList) {
				if ("P00015".equals(hConfig.getPlatNo())) {
					//不正常人类研究中心
					ReptileUtil.getMessage(hCrawlLogs, hConfig, "不正常人类研究中心", dealReptileDao);
				}
			}

			if (hCrawlLogs.size() > 0) {
				logger.info("开始插入爬虫记录表");
				dealReptileDao.insertCrawlLog(hCrawlLogs);
			}
		} else {
			logger.info("=========爬虫配置开关没有开启========");
		}

	}

	/**
	 * Gets wechat data.
	 *
	 * @param dealReptileDao the deal reptile dao
	 * @param hConfig        the h config
	 * @throws Exception the exception
	 */
	public static void getWeChatData(DealReptileDao dealReptileDao, HConfig hConfig) throws Exception {

		//根据网址获取首页html信息,并排除不符合日期和关键字的文章
		ArrayList<Map<String, String>> indexHtml = ReptileUtil.getIndexHtml(dealReptileDao,hConfig);

		//微信获取文章内容并执行插入
		ReptileUtil.getWeChatMessage(hConfig,dealReptileDao,indexHtml,hyzUrl);
	}


}
	/*public void getVideoData() throws Exception {

		String htmlData = ReptileUtil.getHtmlData("https://c.open.163.com/opensg/opensg.do?callback=jQuery191010337061973298978_1505439749127&uuid=843ead00-9921-11e7-be76-95e8b3a3f680&count=4");


		Document src = Jsoup.connect("http://open.163.com/movie/2017/7/V/4/MCP6FINQI_MCP6HEEV4.html")
				.ignoreContentType(true)
				.timeout(10000)
				.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0")
				.get();

		String data = src.data();
		String s = data.substring(data.indexOf("appsrc"));
		String http1 = s.substring(s.indexOf("http")).split("',")[0];
		String mp4 = http1.substring(0, http1.lastIndexOf(".")) + ".mp4";

		ReptileUtil.download(mp4, "1.mp4", "/Users/yiming/Desktop");


	}*/
